package com.socio.resources;

import java.util.Calendar;
import java.util.Date;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.socio.behaviors.DAOBehavior;
import com.socio.behaviors.UserFacebookIDBehavior;
import com.socio.model.Day;
import com.socio.model.User;

@Path("/test")
public class TestResource {

	private final int NUM_DAY_SEC = 86400000;
	
	//@Path("/populate")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
	public String populateDB() {
		
        DAOBehavior userDAO = new UserFacebookIDBehavior();
		
        // 3 top users and then not me
		User user1 = new User();
		user1.setAndroidUDID("000000000000001");
		user1.setNickname("Bob");
		user1.setQuickScore(10000);
		User user2 = new User();
		user2.setAndroidUDID("000000000000002");
		user2.setNickname("Bill");
		user2.setQuickScore(5000);
		User user3 = new User();
		user3.setAndroidUDID("000000000000003");
		user3.setNickname("Big Boss Billy Bob Thorton");
		user3.setQuickScore(3000);
		User user4 = new User();
		user4.setAndroidUDID("000000000000004");
		user4.setQuickScore(220);
		
		Day day1 =  new Day();
		day1.setQuickScore(1000);
		Day day2 = new Day();
		day2.setQuickScore(500);
		Day day3 = new Day();
		day3.setQuickScore(220);
		Day day4 = new Day();
		day4.setQuickScore(100);
		
		// 3 top days and another for me today
		long todayTime = getTodayTime();
		
		day1.setStartTime(todayTime);
		user1.getDays().add(day1);
		userDAO.create(user1);
		user1.getDays().clear();
		day1 = resetDay(day1);
		
		day2.setStartTime(todayTime);
		user2.getDays().add(day2);
		userDAO.create(user2);
		user2.getDays().clear();
		day2 = resetDay(day2);
		
		day3.setStartTime(todayTime);
		user3.getDays().add(day3);
		userDAO.create(user3);
		user3.getDays().clear();
		day3 = resetDay(day3);
		
		day4.setStartTime(todayTime);
		user4.getDays().add(day4);
		userDAO.create(user4);
		user4.getDays().clear();
		day4 = resetDay(day4);
		
		// 3 top days me as number 2 and another day yesterday
		long yesterdayTime = previousDayFromTime(todayTime);
		
		day1.setStartTime(yesterdayTime);
		user1.getDays().add(day1);
		userDAO.create(user1);
		user1.getDays().clear();
		day1 = resetDay(day1);
		
		day3.setStartTime(yesterdayTime);
		user2.getDays().add(day3);
		userDAO.create(user2);
		user2.getDays().clear();
		day3 = resetDay(day3);
		
		day4.setStartTime(yesterdayTime);
		user3.getDays().add(day4);
		userDAO.create(user3);
		user3.getDays().clear();
		day4 = resetDay(day4);
		
		day2.setStartTime(yesterdayTime);
		user4.getDays().add(day2);
		userDAO.create(user4);
		user4.getDays().clear();
		day2 = resetDay(day2);
		
		// 1 day which is me 2 days ago
		long twoDaysAgoTime = previousDayFromTime(yesterdayTime);
		
		day1.setStartTime(twoDaysAgoTime);
		user4.getDays().add(day1);
		userDAO.create(user4);
		user4.getDays().clear();
		day1 = resetDay(day1);
		
		// 3 top days and me with 0 pts 3 days ago
		long threeDaysAgoTime = previousDayFromTime(twoDaysAgoTime);
		
		day1.setStartTime(threeDaysAgoTime);
		user1.getDays().add(day1);
		userDAO.create(user1);
		user1.getDays().clear();
		day1 = resetDay(day1);
		
		day2.setStartTime(threeDaysAgoTime);
		user2.getDays().add(day2);
		userDAO.create(user2);
		user2.getDays().clear();
		day2 = resetDay(day2);
		
		day3.setStartTime(threeDaysAgoTime);
		user3.getDays().add(day3);
		userDAO.create(user3);
		user3.getDays().clear();
		day3 = resetDay(day3);
		
		return "done";
	}
	
	public Day resetDay(Day day){
		day.setUser(null);
		day.setKey(null);
		
		return day;
	}
	
	// Methods explain in Android project
	
	public long getTodayTime() {
		return getDayTimeContainingTime(System.currentTimeMillis());
	}
	
	public long previousDayFromTime(long time) {
		long dayTimeOfParamater = getDayTimeContainingTime(time);
		return dayTimeOfParamater - NUM_DAY_SEC;
	}
	
	public long nextDayFromTime(long time) {
		long dayTimeOfParamater = getDayTimeContainingTime(time);
		return dayTimeOfParamater + NUM_DAY_SEC;
	}
	
	public long getDayTimeContainingTime(long time) {
		return time - (time % NUM_DAY_SEC);
	}
	
	public String dayTimeToString(long time) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(new Date(time));
		
		String dateString = ""+ (calendar.get(Calendar.MONTH)+1); 
		dateString += "/"+ calendar.get(Calendar.DAY_OF_MONTH);
		dateString += "/"+ calendar.get(Calendar.YEAR);

		return dateString;
	}
}
